home *** CD-ROM | disk | FTP | other *** search
- /* Sort torture test */
-
- #include <stdio.h>
- #include <string.h>
- #include "list.h"
- #include "time.h"
-
- typedef struct catalog {
- char number[35];
- char title[40];
- char author[35];
- int date;
- } catalog;
-
- static int compare_author();
- static int compare_title();
- static void load_it();
- static void print_some();
- static time_t what_time();
- static void prRap();
-
- #ifdef ANSI
- static int
- compare_author(catalog *a, catalog *b)
- #else
- static int
- compare_author(a, b)
- catalog *a, *b;
- #endif
- {
- int k;
-
- k = strcmp(a->author, b->author);
-
- if (k == 0)
- return(lSAME);
- else if (k > 0)
- return(l2LT1);
- else if (k < 0)
- return(l1LT2);
- }
-
- #ifdef ANSI
- static int
- compare_title(catalog *a, catalog *b, int order)
- #else
- static int
- compare_title(a, b, order)
- catalog *a, *b;
- int order;
- #endif
- {
- int k;
-
- k = strcmp(a->title, b->title);
-
- if (k == 0)
- return(lSAME);
- else if (k > 0)
- return(l2LT1);
- else if (k < 0)
- return(l1LT2);
- }
-
- #ifdef ANSI
- static void
- load_it(int id, int num)
- #else
- static void
- load_it(id, num)
- int id;
- int num;
- #endif
- {
- int loop, size = sizeof(catalog), code;
- char c1, c2, c3, c4, c5, c6, c7, c8;
- catalog rap;
-
- c1 = c2 = c3 = c4 = c5 = c6 = c7 = 'A';
- c7--;
- for (loop=1; loop<=num; loop++) {
- c7++;
-
- if (c7>'Z') {c7 = 'A'; c6++;}
- if (c6>'Z') {c6 = 'A'; c5++;}
- if (c5>'Z') {c5 = 'A'; c4++;}
- if (c4>'Z') {c4 = 'A'; c3++;}
- if (c3>'Z') {c3 = 'A'; c2++;}
- if (c2>'Z') {c2 = 'A'; c1++;}
- if (c1>'Z') {c1 = 'A'; c1 = c2 = c3 = c4 = c5 = c6 = c7 = 'A';}
-
- sprintf(rap.number,"B-90-%d", loop);
- sprintf(rap.title, "Book %d", loop);
- sprintf(rap.author,"%c%c%c%c%c%c%c%d",
- c1, c2, c3, c4, c5, c6, c7, loop);
- rap.date = 890129;
- code = lInsNode(id, lLAST, &rap, size, loop);
- }
- /* printf("Linked List created\n"); */
- }
-
- #ifdef ANSI
- static void
- print_some(int id, int num)
- #else
- static void
- print_some(id, num)
- int id;
- int num;
- #endif
- {
- int size = sizeof(catalog), loop;
- int code;
- catalog rap;
-
- code = lGetNode(id, lFIRST, &rap, size);
- prRap(code, &rap);
-
- for (loop=2; loop<=num; loop++) {
- code = lGetNode(id, lNEXT, &rap, size);
- prRap(code, &rap);
- }
- }
-
- #ifdef ANSI
- static time_t what_time(void)
- #else
- static time_t what_time()
- #endif
- {
- return((time_t)time(NULL));
- }
-
- #ifdef ANSI
- static void diff_time(time_t a, time_t b)
- #else
- static void diff_time(a, b)
- time_t a, b;
- #endif
- {
- struct tm *tt;
- char buf1[101], buf2[101];
- int hour[2], min[2], sec[2];
- time_t x;
-
- tt = (struct tm *)malloc(sizeof(struct tm *) + 1);
-
- tt = (struct tm *)localtime(&a);
- strftime(buf1, 100, "%H %M %S", tt);
-
- tt = (struct tm *)localtime(&b);
- strftime(buf2, 100, "%H %M %S", tt);
-
- sscanf(buf1,"%d %d %d", &hour[0], &min[0], &sec[0]);
- sscanf(buf2,"%d %d %d", &hour[1], &min[1], &sec[1]);
-
- printf("Hours = %d Mins = %d Secs = %d\n",
- (hour[1] - hour[0] > 0)?(hour[1] - hour[0]) : (hour[0] - hour[1]),
- (min[1] - min[0] > 0)?(min[1] - min[0]) : (min[0] - min[1]),
- (sec[1] - sec[0] > 0)?(sec[1] - sec[0]) : (sec[0] - sec[1]));
- }
-
- main()
- {
- int size = sizeof(catalog);
- int id, code;
- time_t start, finish;
- catalog rap;
-
- /* Quick sort */
- /* start testing sorting efficiency */
- id = lDef(lSINGLY, lCHAIN);
- load_it(id, 3000);
-
- printf("Untouched list\n");
- print_some(id, 10);
-
- printf("-- lQuickSort --\nlQuickSort DESCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lDESCENDING, lQUICK, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lQuickSort ASCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lASCENDING, lQUICK, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lQuickSort DESCENDING by AUTHOR : ");
- start = what_time();
- code = lSort(id, lDESCENDING, lQUICK, compare_author);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lQuickSort ASCENDING by AUTHOR : ");
- start = what_time();
- code = lSort(id, lASCENDING, lQUICK, compare_author);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- code = lDelAll();
-
- /* Heap sort */
- /* start testing sorting efficiency */
- id = lDef(lSINGLY, lCHAIN);
- load_it(id, 3000);
-
- printf("Untouched list\n");
- print_some(id, 10);
-
- printf(" -- lHeapSort -- \nlHeapSort DESCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lDESCENDING, lHEAP, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lHeapSort ASCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lASCENDING, lHEAP, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lHeapSort ASCENDING by AUTHOR : ");
- start = what_time();
- code = lSort(id, lASCENDING, lHEAP, compare_author);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- code = lDelAll();
-
- /* Selection sort */
- /* start testing sorting efficiency */
- id = lDef(lSINGLY, lCHAIN);
- load_it(id, 3000);
-
- printf("Untouched list\n");
- print_some(id, 10);
-
- printf("lSelectionSort DESCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lDESCENDING, lSELECTION, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lSelectionSort ASCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lASCENDING, lSELECTION, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lSelectionSort ASCENDING by AUTHOR : ");
- start = what_time();
- code = lSort(id, lASCENDING, lSELECTION, compare_author);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- code = lDelAll();
-
- /* insertion sort*/
- /* start testing sorting efficiency */
- id = lDef(lSINGLY, lCHAIN);
- load_it(id, 3000);
-
- printf("Untouched list\n");
- print_some(id, 10);
-
- printf("lInsertSort DESCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lDESCENDING, lINSERT, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lInsertSort ASCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lASCENDING, lINSERT, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lInsertSort ASCENDING by AUTHOR : ");
- start = what_time();
- code = lSort(id, lASCENDING, lINSERT, compare_author);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- code = lDelAll();
-
- /* start testing sorting efficiency */
- id = lDef(lSINGLY, lCHAIN);
- load_it(id, 3000);
-
- printf("Untouched list\n");
- print_some(id, 10);
-
- /* Bubble sort */
- printf("lBubbleSort DESCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lDESCENDING, lBUBBLE, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lBubbleSort ASCENDING by TITLE : ");
- start = what_time();
- code = lSort(id, lASCENDING, lBUBBLE, compare_title);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- printf("lBubbleSort ASCENDING by AUTHOR : ");
- start = what_time();
- code = lSort(id, lASCENDING, lBUBBLE, compare_author);
- finish = what_time();
- diff_time(start, finish);
- print_some(id, 10);
-
- code = lDelAll();
- }
-
- static void
- prRap(code, rpprt)
- int code;
- catalog *rpprt;
- {
- if (code >= 0)
- printf("catalog :'%s' '%s' '%s' '%d'\n", rpprt->number, rpprt->title,
- rpprt->author, rpprt->date);
- else
- printf("Return code = %d\n", code);
- }
-